package com.qianbei.user;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.bumptech.glide.Glide;
import com.qianbei.R;
import com.qianbei.common.base.BaseActivity;
import com.qianbei.common.base.BaseFragment;
import com.qianbei.common.init.SetData;
import com.qianbei.common.net.control.Constant;
import com.qianbei.common.net.control.ServerControl;
import com.qianbei.common.net.control.ServerResult;
import com.qianbei.common.rxbus.RxBus;
import com.qianbei.common.utilis.img.CropCircleTransformation;
import com.qianbei.common.web.WebActivity;
import com.qianbei.user.feedback.FeedBackActivity;
import com.qianbei.user.income.MyIncomeActivity;
import com.qianbei.user.income.ShareIncomeActivity;
import com.qianbei.user.invitationCode.InvitationCodeActivity;
import com.qianbei.user.person.PersonBean;
import com.qianbei.user.person.PersonalInformationActivity;

import rx.Subscription;
import rx.functions.Action1;

/**
 * 用户个人中心
 * <p/>
 * Created by user on 16/9/4.
 */
public class UserCenterNewFragment extends BaseFragment implements View.OnClickListener {

    private View mRootView;

    private LinearLayout mMyTaskSystem;
    private ImageView mIvUserIcon;
    private TextView mTvNotification;
    private TextView mTvUserName;
    private TextView mTvSetInfo;
    private TextView mTvUserGold;
    private LinearLayout mLlUserGold;
    private TextView mTvUserMoney;
    private LinearLayout mLlUserMoney;
    private TextView mTvUserPrentice;
    private LinearLayout mLlUserPrentice;
    private RelativeLayout mTopLayout;
    private LinearLayout mMyInviteFriends;
    private LinearLayout mMyInvitationCode;
    private LinearLayout mMyDetailIncome;
    private LinearLayout mMyInInput;
    private LinearLayout mMyCashWithDrawal;
    private LinearLayout mMyNewGuide;
    private LinearLayout mMyFeedBack;
    private Subscription subscription;
    private TextView mTvSetting;
    private String uppercode;


    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (mRootView == null) {
            mRootView = inflater.inflate(R.layout.fragment_user_center_new, container, false);
        }

        initView();
        initGetData();

        subscription = RxBus.getDefault().toObservable(String.class)
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        if (s.equals("userinfo")) {
                            initGetData();
                        } else if (s.equals("getUserInfo")) {
                            initGetData();
                        }
                    }
                });


        return mRootView;
    }

    private void initGetData() {
        ServerControl sc = new ServerControl(Request.Method.GET, GET_PERSONAL_INFO + SetData.getUserID() + "?");

        sc.serverListener = new ServerControl.ServerListener() {
            @Override
            public void serverFinish(ServerResult sr) {
                if (sr.isContinue) {
                    PersonBean personBean = JSON.parseObject(sr.bodyData.toString(), PersonBean.class);

                    String headimg = personBean.getData().getHeadimg();
                    String username = personBean.getData().getUsername();
                    int point = personBean.getData().getPoint();
                    double money = personBean.getData().getMoney();
                    int invinum = personBean.getData().getInvinum();
                    uppercode = personBean.getData().getUppercode();

                    if (uppercode.equals("0")) {
                        mMyInvitationCode.setVisibility(View.VISIBLE);
                    } else {
                        mMyInvitationCode.setVisibility(View.GONE);
                    }
                    mTvUserName.setText(username);
                    mTvUserGold.setText(String.valueOf(point));
                    mTvUserMoney.setText(String.valueOf(money));
                    mTvUserPrentice.setText(String.valueOf(invinum));
                    if (!mContext.isFinishing()) {
                        Glide.with(mContext)
                                .load(headimg)
                                .bitmapTransform(new CropCircleTransformation(mContext))
                                .placeholder(R.drawable.ic_launcher)
                                .into(mIvUserIcon);
                    }

                }
            }
        };

        sc.startVolley();
    }

    private void initView() {
        mTvSetting = (TextView) mRootView.findViewById(R.id.tv_setting);
        mTvSetting.setOnClickListener(this);
        mMyTaskSystem = (LinearLayout) mRootView.findViewById(R.id.my_task_system);
        mMyTaskSystem.setOnClickListener(this);
        mIvUserIcon = (ImageView) mRootView.findViewById(R.id.iv_user_icon);
        mIvUserIcon.setOnClickListener(this);
        mTvNotification = (TextView) mRootView.findViewById(R.id.tv_notification);
        mTvNotification.setOnClickListener(this);
        mTvUserName = (TextView) mRootView.findViewById(R.id.tv_user_name);
        mTvUserName.setOnClickListener(this);
        mTvSetInfo = (TextView) mRootView.findViewById(R.id.tv_set_info);
        mTvSetInfo.setOnClickListener(this);
        mTvUserGold = (TextView) mRootView.findViewById(R.id.tv_user_gold);
        mTvUserGold.setOnClickListener(this);
        mLlUserGold = (LinearLayout) mRootView.findViewById(R.id.ll_user_gold);
        mLlUserGold.setOnClickListener(this);
        mTvUserMoney = (TextView) mRootView.findViewById(R.id.tv_user_money);
        mTvUserMoney.setOnClickListener(this);
        mLlUserMoney = (LinearLayout) mRootView.findViewById(R.id.ll_user_money);
        mLlUserMoney.setOnClickListener(this);
        mTvUserPrentice = (TextView) mRootView.findViewById(R.id.tv_user_prentice);
        mTvUserPrentice.setOnClickListener(this);
        mLlUserPrentice = (LinearLayout) mRootView.findViewById(R.id.ll_user_prentice);
        mLlUserPrentice.setOnClickListener(this);
        mTopLayout = (RelativeLayout) mRootView.findViewById(R.id.top_layout);
        mTopLayout.setOnClickListener(this);
        mMyInviteFriends = (LinearLayout) mRootView.findViewById(R.id.my_invite_friends);
        mMyInviteFriends.setOnClickListener(this);
        mMyInvitationCode = (LinearLayout) mRootView.findViewById(R.id.my_invitation_code);
        mMyInvitationCode.setOnClickListener(this);
        mMyDetailIncome = (LinearLayout) mRootView.findViewById(R.id.my_detail_income);
        mMyDetailIncome.setOnClickListener(this);
        mMyInInput = (LinearLayout) mRootView.findViewById(R.id.my_in_input);
        mMyInInput.setOnClickListener(this);
        mMyCashWithDrawal = (LinearLayout) mRootView.findViewById(R.id.my_cash_withdrawal);
        mMyCashWithDrawal.setOnClickListener(this);
        mMyNewGuide = (LinearLayout) mRootView.findViewById(R.id.my_new_guide);
        mMyNewGuide.setOnClickListener(this);
        mMyFeedBack = (LinearLayout) mRootView.findViewById(R.id.my_feed_back);
        mMyFeedBack.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        super.onClick(view);

        switch (view.getId()) {
            case R.id.iv_user_icon:
                startActivity(PersonalInformationActivity.class);
                break;
            case R.id.tv_user_name:
                startActivity(PersonalInformationActivity.class);
                break;
            case R.id.tv_set_info:
                startActivity(PersonalInformationActivity.class);
                break;
            case R.id.my_invite_friends:
                startActivity(ExampleActivity.class);
                break;
            case R.id.tv_setting:
                startActivity(SetActivity.class);
                break;
            case R.id.top_layout:
                startActivity(PersonalInformationActivity.class);
                break;
            case R.id.my_task_system:
                break;
            case R.id.my_invitation_code:
                startActivity(InvitationCodeActivity.class);
                break;
            case R.id.my_detail_income:
                startActivity(MyIncomeActivity.class);
                break;
            case R.id.my_in_input:
                startActivity(ShareIncomeActivity.class);
                break;
            case R.id.my_cash_withdrawal:
                break;
            case R.id.my_new_guide:
                WebActivity.startWebActivity(getActivity(), Constant.URL + "user/zhinan");
                break;
            case R.id.my_feed_back:
                startActivity(FeedBackActivity.class);
                break;
            case R.id.ll_user_gold:
                startActivity(MyIncomeActivity.class);
                break;
            case R.id.ll_user_money:
                startActivity(MyIncomeActivity.class);
                break;
            case R.id.ll_user_prentice:
                startActivity(ExampleActivity.class);
                break;
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (!subscription.isUnsubscribed()) {
            subscription.unsubscribe();
        }
    }
}
